package com.mingqijia.gassafety.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.mingqijia.gassafety.db.entity.AppealLog;
import com.mingqijia.gassafety.db.entity.HistoryAlarm;
import com.mingqijia.gassafety.db.entity.HistoryBroken;
import com.mingqijia.gassafety.db.entity.vo.BatchAppealVO;
import com.mingqijia.gassafety.db.entity.vo.HistoryBrokenVO;
import com.mingqijia.gassafety.db.entity.vo.LatestLogListVO;
import com.mingqijia.gassafety.db.mapper.AppealLogMapper;
import com.mingqijia.gassafety.db.mapper.HistoryBrokenMapper;
import com.mingqijia.gassafety.job.service.AppealLogService;
import com.mingqijia.gassafety.job.service.HistoryAlarmService;
import com.mingqijia.gassafety.job.service.HistoryBrokenService;
import com.mingqijia.gassafety.shared.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 报警器诉求记录表 服务实现类
 * </p>
 *
 * @author guoliang.lao
 * @since 2023-05-17
 */
@Service
@Slf4j
public class AppealLogServiceImpl extends ServiceImpl<AppealLogMapper, AppealLog> implements AppealLogService {

    @Autowired
    HistoryBrokenMapper historyBrokenMapper;
    @Autowired
    HistoryAlarmService historyAlarmService;
    @Autowired
    HistoryBrokenService historyBrokenService;

    @Override
    @Async
    public void batchAppealBrokenLog(BatchAppealVO batchAppealVO) {
        log.info("批量处理开始,入参为:{}",batchAppealVO);
        LatestLogListVO latestLogListVO = new LatestLogListVO();
        latestLogListVO.setImei(batchAppealVO.getImei());
        int page = batchAppealVO.getPage();
        int pageSize = batchAppealVO.getPageSize();
        while (true){
            PageHelper.startPage(page, pageSize);
            List<HistoryBrokenVO> vos = historyBrokenMapper.getVccRecord(latestLogListVO);
            if (CollectionUtils.isEmpty(vos)){
                break;
            }
            List<AppealLog> appealLogs = new ArrayList<>();
            for (HistoryBrokenVO vo : vos) {
                AppealLog appealLog = new AppealLog();
                appealLog.setAppealResult(batchAppealVO.getAppealResult());
                appealLog.setAlarmBrokenId(vo.getId());
                appealLog.setUserAccount(batchAppealVO.getUserName());
                appealLog.setUserName(batchAppealVO.getUserName());
                appealLog.setAppealStatus(Constants.APPEAL_STATUS_2);
                appealLog.setAppealType(1);
                appealLog.setSpId(batchAppealVO.getSpId());
                appealLogs.add(appealLog);
            }
            this.saveBatch(appealLogs);
            for (AppealLog appealLog : appealLogs) {
                // 更新报警/故障信息记录主表
                if (appealLog.getAppealType() != null || appealLog.getAppealType() == 0) {
                    LambdaUpdateWrapper<HistoryAlarm> historyAlarmWrapper = new LambdaUpdateWrapper<>();
                    historyAlarmWrapper.eq(HistoryAlarm::getId,appealLog.getAlarmBrokenId()).eq(HistoryAlarm::getIsDeleted,0);
                    HistoryAlarm historyAlarmLog = new HistoryAlarm();
                    historyAlarmLog.setWorkorderId(appealLog.getWorkorderId() == null ? 0L : appealLog.getWorkorderId());
                    historyAlarmLog.setAppealStatus(appealLog.getAppealStatus());
                    historyAlarmLog.setAppealResult(appealLog.getAppealResult());
                    historyAlarmLog.setUserName(appealLog.getUserName());
                    historyAlarmLog.setUserAccount(appealLog.getUserAccount());
                    historyAlarmLog.setAppealTime(appealLog.getCreatedAt());
                    boolean historyAlarmResult = historyAlarmService.update(historyAlarmLog,historyAlarmWrapper);
                    log.info("工单处理完成，更新报警器设备报警记录,结果：{}，内容：{}",historyAlarmResult,historyAlarmLog.toString());
                } else if (appealLog.getAppealType() == 1) {
                    LambdaUpdateWrapper<HistoryBroken> historyBrokenWrapper = new LambdaUpdateWrapper<>();
                    historyBrokenWrapper.eq(HistoryBroken::getId,appealLog.getAlarmBrokenId()).eq(HistoryBroken::getIsDeleted,0);
                    HistoryBroken historyBrokenLog = new HistoryBroken();
                    historyBrokenLog.setWorkorderId(appealLog.getWorkorderId() == null ? 0L : appealLog.getWorkorderId());
                    historyBrokenLog.setAppealStatus(appealLog.getAppealStatus());
                    historyBrokenLog.setAppealResult(appealLog.getAppealResult());
                    historyBrokenLog.setUserName(appealLog.getUserName());
                    historyBrokenLog.setUserAccount(appealLog.getUserAccount());
                    historyBrokenLog.setAppealTime(appealLog.getCreatedAt());
                    boolean historyBrokenResult = historyBrokenService.update(historyBrokenLog,historyBrokenWrapper);
                    log.info("工单处理完成，更新报警器设备故障记录,结果：{}，内容：{}",historyBrokenResult,historyBrokenLog.toString());
                }
            }
            page = page + 1;
        }
        log.info("批量处理结束,设备号为:{}",batchAppealVO.getImei());
    }
}
